<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>2.4 options | 引言</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../c02/s05.html" />
    
    
    <link rel="prev" href="../c02/s03.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="2.4"
        data-chapter-title="2.4 options"
        data-filepath="c02/s04.md"
        data-basepath=".."
        data-revision="Fri Feb 10 2017 17:36:30 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        引言
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="c01/index.html">
            
                
                    <a href="../c01/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        1 关于Tornado
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="c01/s01.html">
            
                
                    <a href="../c01/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        1.1 Tornado是为何物
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="c01/s02.html">
            
                
                    <a href="../c01/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        1.2 Tornado与Django
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="c02/index.html">
            
                
                    <a href="../c02/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        2 初识Tornado
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="c02/s01.html">
            
                
                    <a href="../c02/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        2.1 安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="c02/s02.html">
            
                
                    <a href="../c02/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        2.2 Hello Itcast
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="c02/s03.html">
            
                
                    <a href="../c02/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        2.3 httpserver
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="2.4" data-path="c02/s04.html">
            
                
                    <a href="../c02/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        2.4 options
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="c02/s05.html">
            
                
                    <a href="../c02/s05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        2.5 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="c03/index.html">
            
                
                    <a href="../c03/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        3 深入Tornado
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="c03/s01.html">
            
                
                    <a href="../c03/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        3.1 Application
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="c03/s02.html">
            
                
                    <a href="../c03/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        3.2 输入
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="c03/s03.html">
            
                
                    <a href="../c03/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        3.3 输出
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="c03/s04.html">
            
                
                    <a href="../c03/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        3.4 接口与调用顺序
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="c03/s05.html">
            
                
                    <a href="../c03/s05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        3.5 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="c04/index.html">
            
                
                    <a href="../c04/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        4 模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="c04/s01.html">
            
                
                    <a href="../c04/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        4.1 静态文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="c04/s02.html">
            
                
                    <a href="../c04/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        4.2 使用模板
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="c04/s03.html">
            
                
                    <a href="../c04/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        4.3 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="c05/index.html">
            
                
                    <a href="../c05/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        5 数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="c05/s01.html">
            
                
                    <a href="../c05/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        5.1 数据库
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="c05/s02.html">
            
                
                    <a href="../c05/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        5.2 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="c06/index.html">
            
                
                    <a href="../c06/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        6 安全应用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="c06/s01.html">
            
                
                    <a href="../c06/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        6.1 Cookie
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="c06/s02.html">
            
                
                    <a href="../c06/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        6.2 XSRF
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="c06/s03.html">
            
                
                    <a href="../c06/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        6.3 用户验证
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="c06/s04.html">
            
                
                    <a href="../c06/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        6.4 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="c07/index.html">
            
                
                    <a href="../c07/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        7 异步与WebSocket
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="c07/s01.html">
            
                
                    <a href="../c07/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        7.1 认识异步
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="c07/s02.html">
            
                
                    <a href="../c07/s02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        7.2 Tornado异步
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="c07/s03.html">
            
                
                    <a href="../c07/s03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        7.3 WebSocket
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="c07/s04.html">
            
                
                    <a href="../c07/s04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        7.4 练习
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="c08/index.html">
            
                
                    <a href="../c08/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        8 部署
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="c08/s01.html">
            
                
                    <a href="../c08/s01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        8.1 部署Tornado
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >引言</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="24-options">2.4 options</h1>
<p>&#x5728;&#x524D;&#x9762;&#x7684;&#x793A;&#x4F8B;&#x4E2D;&#x6211;&#x4EEC;&#x90FD;&#x662F;&#x5C06;&#x670D;&#x52A1;&#x7AEF;&#x53E3;&#x7684;&#x53C2;&#x6570;&#x5199;&#x6B7B;&#x5728;&#x7A0B;&#x5E8F;&#x4E2D;&#xFF0C;&#x5F88;&#x4E0D;&#x7075;&#x6D3B;&#x3002;</p>
<p>tornado&#x4E3A;&#x6211;&#x4EEC;&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x4E2A;&#x4FBF;&#x6377;&#x7684;&#x5DE5;&#x5177;&#xFF0C;<strong>tornado.options&#x6A21;&#x5757;&#x2014;&#x2014;&#x5168;&#x5C40;&#x53C2;&#x6570;&#x5B9A;&#x4E49;&#x3001;&#x5B58;&#x50A8;&#x3001;&#x8F6C;&#x6362;</strong>&#x3002;</p>
<h2 id="tornadooptionsdefine">tornado.options.define()</h2>
<p>&#x7528;&#x6765;&#x5B9A;&#x4E49;options&#x9009;&#x9879;&#x53D8;&#x91CF;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x5B9A;&#x4E49;&#x7684;&#x53D8;&#x91CF;&#x53EF;&#x4EE5;&#x5728;&#x5168;&#x5C40;&#x7684;tornado.options.options&#x4E2D;&#x83B7;&#x53D6;&#x4F7F;&#x7528;&#xFF0C;&#x4F20;&#x5165;&#x53C2;&#x6570;&#xFF1A;</p>
<ul>
<li><strong>name</strong> &#x9009;&#x9879;&#x53D8;&#x91CF;&#x540D;&#xFF0C;<strong>&#x987B;&#x4FDD;&#x8BC1;&#x5168;&#x5C40;&#x552F;&#x4E00;&#x6027;</strong>&#xFF0C;&#x5426;&#x5219;&#x4F1A;&#x62A5;&#x201C;Option &apos;xxx&apos; already defined in ...&#x201D;&#x7684;&#x9519;&#x8BEF;&#xFF1B;</li>
<li><strong>default</strong>&#x3000;&#x9009;&#x9879;&#x53D8;&#x91CF;&#x7684;&#x9ED8;&#x8BA4;&#x503C;&#xFF0C;&#x5982;&#x4E0D;&#x4F20;&#x9ED8;&#x8BA4;&#x4E3A;None&#xFF1B;</li>
<li><strong>type</strong> &#x9009;&#x9879;&#x53D8;&#x91CF;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x4ECE;&#x547D;&#x4EE4;&#x884C;&#x6216;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x5BFC;&#x5165;&#x53C2;&#x6570;&#x7684;&#x65F6;&#x5019;tornado&#x4F1A;&#x6839;&#x636E;&#x8FD9;&#x4E2A;&#x7C7B;&#x578B;&#x8F6C;&#x6362;&#x8F93;&#x5165;&#x7684;&#x503C;&#xFF0C;&#x8F6C;&#x6362;&#x4E0D;&#x6210;&#x529F;&#x65F6;&#x4F1A;&#x62A5;&#x9519;&#xFF0C;&#x53EF;&#x4EE5;&#x662F;str&#x3001;float&#x3001;int&#x3001;datetime&#x3001;timedelta&#x4E2D;&#x7684;&#x67D0;&#x4E2A;&#xFF0C;&#x82E5;&#x672A;&#x8BBE;&#x7F6E;&#x5219;&#x6839;&#x636E;default&#x7684;&#x503C;&#x81EA;&#x52A8;&#x63A8;&#x65AD;&#xFF0C;&#x82E5;default&#x4E5F;&#x672A;&#x8BBE;&#x7F6E;&#xFF0C;&#x90A3;&#x4E48;&#x4E0D;&#x518D;&#x8FDB;&#x884C;&#x8F6C;&#x6362;&#x3002;<strong>&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x5229;&#x7528;&#x8BBE;&#x7F6E;type&#x7C7B;&#x578B;&#x5B57;&#x6BB5;&#x6765;&#x8FC7;&#x6EE4;&#x4E0D;&#x6B63;&#x786E;&#x7684;&#x8F93;&#x5165;&#x3002;</strong></li>
<li><strong>multiple</strong> &#x9009;&#x9879;&#x53D8;&#x91CF;&#x7684;&#x503C;&#x662F;&#x5426;&#x53EF;&#x4EE5;&#x4E3A;&#x591A;&#x4E2A;&#xFF0C;&#x5E03;&#x5C14;&#x7C7B;&#x578B;&#xFF0C;&#x9ED8;&#x8BA4;&#x503C;&#x4E3A;False&#xFF0C;&#x5982;&#x679C;multiple&#x4E3A;True&#xFF0C;&#x90A3;&#x4E48;&#x8BBE;&#x7F6E;&#x9009;&#x9879;&#x53D8;&#x91CF;&#x65F6;&#x503C;&#x4E0E;&#x503C;&#x4E4B;&#x95F4;&#x7528;&#x82F1;&#x6587;&#x9017;&#x53F7;&#x5206;&#x9694;&#xFF0C;&#x800C;&#x9009;&#x9879;&#x53D8;&#x91CF;&#x5219;&#x662F;&#x4E00;&#x4E2A;list&#x5217;&#x8868;&#xFF08;&#x82E5;&#x9ED8;&#x8BA4;&#x503C;&#x548C;&#x8F93;&#x5165;&#x5747;&#x672A;&#x8BBE;&#x7F6E;&#xFF0C;&#x5219;&#x4E3A;&#x7A7A;&#x5217;&#x8868;[]&#xFF09;&#x3002;</li>
<li><strong>help</strong> &#x9009;&#x9879;&#x53D8;&#x91CF;&#x7684;&#x5E2E;&#x52A9;&#x63D0;&#x793A;&#x4FE1;&#x606F;&#xFF0C;&#x5728;&#x547D;&#x4EE4;&#x884C;&#x542F;&#x52A8;tornado&#x65F6;&#xFF0C;&#x901A;&#x8FC7;&#x52A0;&#x5165;&#x547D;&#x4EE4;&#x884C;&#x53C2;&#x6570; --help&#x3000;&#x53EF;&#x4EE5;&#x67E5;&#x770B;&#x6240;&#x6709;&#x9009;&#x9879;&#x53D8;&#x91CF;&#x7684;&#x4FE1;&#x606F;&#xFF08;&#x6CE8;&#x610F;&#xFF0C;&#x4EE3;&#x7801;&#x4E2D;&#x9700;&#x8981;&#x52A0;&#x5165;tornado.options.parse_command_line()&#xFF09;&#x3002;</li>
</ul>
<h2 id="tornadooptionsoptions">tornado.options.options</h2>
<p>&#x5168;&#x5C40;&#x7684;options&#x5BF9;&#x8C61;&#xFF0C;&#x6240;&#x6709;&#x5B9A;&#x4E49;&#x7684;&#x9009;&#x9879;&#x53D8;&#x91CF;&#x90FD;&#x4F1A;&#x4F5C;&#x4E3A;&#x8BE5;&#x5BF9;&#x8C61;&#x7684;&#x5C5E;&#x6027;&#x3002;</p>
<h2 id="tornadooptionsparsecommandline">tornado.options.parse_command_line()</h2>
<p>&#x8F6C;&#x6362;&#x547D;&#x4EE4;&#x884C;&#x53C2;&#x6570;&#xFF0C;&#x5E76;&#x5C06;&#x8F6C;&#x6362;&#x540E;&#x7684;&#x503C;&#x5BF9;&#x5E94;&#x7684;&#x8BBE;&#x7F6E;&#x5230;&#x5168;&#x5C40;options&#x5BF9;&#x8C61;&#x76F8;&#x5173;&#x5C5E;&#x6027;&#x4E0A;&#x3002;&#x8FFD;&#x52A0;&#x547D;&#x4EE4;&#x884C;&#x53C2;&#x6570;&#x7684;&#x65B9;&#x5F0F;&#x662F;--myoption=myvalue</p>
<p>&#x65B0;&#x5EFA;opt.py&#xFF0C;&#x6211;&#x4EEC;&#x7528;&#x4EE3;&#x7801;&#x6765;&#x770B;&#x4E00;&#x4E0B;&#x5982;&#x4F55;&#x4F7F;&#x7528;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># coding:utf-8</span>

<span class="hljs-keyword">import</span> tornado.web
<span class="hljs-keyword">import</span> tornado.ioloop
<span class="hljs-keyword">import</span> tornado.httpserver
<span class="hljs-keyword">import</span> tornado.options <span class="hljs-comment"># &#x65B0;&#x5BFC;&#x5165;&#x7684;options&#x6A21;&#x5757;</span>

tornado.options.define(<span class="hljs-string">&quot;port&quot;</span>, default=<span class="hljs-number">8000</span>, type=int, help=<span class="hljs-string">&quot;run server on the given port.&quot;</span>) <span class="hljs-comment"># &#x5B9A;&#x4E49;&#x670D;&#x52A1;&#x5668;&#x76D1;&#x542C;&#x7AEF;&#x53E3;&#x9009;&#x9879;</span>
tornado.options.define(<span class="hljs-string">&quot;itcast&quot;</span>, default=[], type=str, multiple=<span class="hljs-keyword">True</span>, help=<span class="hljs-string">&quot;itcast subjects.&quot;</span>) <span class="hljs-comment"># &#x65E0;&#x610F;&#x4E49;&#xFF0C;&#x6F14;&#x793A;&#x591A;&#x503C;&#x60C5;&#x51B5;</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IndexHandler</span><span class="hljs-params">(tornado.web.RequestHandler)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x4E3B;&#x8DEF;&#x7531;&#x5904;&#x7406;&#x7C7B;&quot;&quot;&quot;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;&#x5BF9;&#x5E94;http&#x7684;get&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;&quot;&quot;&quot;</span>
        self.write(<span class="hljs-string">&quot;Hello Itcast!&quot;</span>)

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&quot;__main__&quot;</span>:
    tornado.options.parse_command_line()
    <span class="hljs-keyword">print</span> tornado.options.options.itcast <span class="hljs-comment"># &#x8F93;&#x51FA;&#x591A;&#x503C;&#x9009;&#x9879;</span>
    app = tornado.web.Application([
        (<span class="hljs-string">r&quot;/&quot;</span>, IndexHandler),
    ])
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(tornado.options.options.port)
    tornado.ioloop.IOLoop.current().start()
</code></pre>
<p>&#x6267;&#x884C;&#x5982;&#x4E0B;&#x547D;&#x4EE4;&#x5F00;&#x542F;&#x7A0B;&#x5E8F;&#xFF1A;</p>
<pre><code class="lang-bash">$ python opt.py --port=<span class="hljs-number">9000</span> --itcast=python,c++,java,php,ios
</code></pre>
<p>&#x6548;&#x679C;&#x5982;&#x4E0B;&#xFF1A;
<img src="../images/options_demo.png" alt="&#x9009;&#x9879;&#x6F14;&#x793A;"></p>
<h2 id="tornadooptionsparseconfigfilepath">tornado.options.parse_config_file(path)</h2>
<p>&#x4ECE;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x5BFC;&#x5165;option&#xFF0C;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x4E2D;&#x7684;&#x9009;&#x9879;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>myoption = &quot;myvalue&quot;
myotheroption = &quot;myothervalue&quot;
</code></pre><p>&#x6211;&#x4EEC;&#x7528;&#x4EE3;&#x7801;&#x6765;&#x770B;&#x4E00;&#x4E0B;&#x5982;&#x4F55;&#x4F7F;&#x7528;&#xFF0C;&#x65B0;&#x5EFA;&#x914D;&#x7F6E;&#x6587;&#x4EF6;config&#xFF0C;&#x6CE8;&#x610F;&#x5B57;&#x7B26;&#x4E32;&#x548C;&#x5217;&#x8868;&#x6309;&#x7167;python&#x7684;&#x8BED;&#x6CD5;&#x683C;&#x5F0F;&#xFF1A;</p>
<pre><code>port = 8000
itcast = [&quot;python&quot;,&quot;c++&quot;,&quot;java&quot;,&quot;php&quot;,&quot;ios&quot;]
</code></pre><p>&#x4FEE;&#x6539;opt.py&#x6587;&#x4EF6;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># coding:utf-8</span>

<span class="hljs-keyword">import</span> tornado.web
<span class="hljs-keyword">import</span> tornado.ioloop
<span class="hljs-keyword">import</span> tornado.httpserver
<span class="hljs-keyword">import</span> tornado.options <span class="hljs-comment"># &#x65B0;&#x5BFC;&#x5165;&#x7684;options&#x6A21;&#x5757;</span>

tornado.options.define(<span class="hljs-string">&quot;port&quot;</span>, default=<span class="hljs-number">8000</span>, type=int, help=<span class="hljs-string">&quot;run server on the given port.&quot;</span>) <span class="hljs-comment"># &#x5B9A;&#x4E49;&#x670D;&#x52A1;&#x5668;&#x76D1;&#x542C;&#x7AEF;&#x53E3;&#x9009;&#x9879;</span>
tornado.options.define(<span class="hljs-string">&quot;itcast&quot;</span>, default=[], type=str, multiple=<span class="hljs-keyword">True</span>, help=<span class="hljs-string">&quot;itcast subjects.&quot;</span>) <span class="hljs-comment"># &#x65E0;&#x610F;&#x4E49;&#xFF0C;&#x6F14;&#x793A;&#x591A;&#x503C;&#x60C5;&#x51B5;</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">IndexHandler</span><span class="hljs-params">(tornado.web.RequestHandler)</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot;&#x4E3B;&#x8DEF;&#x7531;&#x5904;&#x7406;&#x7C7B;&quot;&quot;&quot;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-string">&quot;&quot;&quot;&#x5BF9;&#x5E94;http&#x7684;get&#x8BF7;&#x6C42;&#x65B9;&#x5F0F;&quot;&quot;&quot;</span>
        self.write(<span class="hljs-string">&quot;Hello Itcast!&quot;</span>)

<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&quot;__main__&quot;</span>:
    tornado.options.parse_config_file(<span class="hljs-string">&quot;./config&quot;</span>) <span class="hljs-comment"># &#x4EC5;&#x4EC5;&#x4FEE;&#x6539;&#x4E86;&#x6B64;&#x5904;</span>
    <span class="hljs-keyword">print</span> tornado.options.options.itcast <span class="hljs-comment"># &#x8F93;&#x51FA;&#x591A;&#x503C;&#x9009;&#x9879;</span>
    app = tornado.web.Application([
        (<span class="hljs-string">r&quot;/&quot;</span>, IndexHandler),
    ])
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(tornado.options.options.port)
    tornado.ioloop.IOLoop.current().start()
</code></pre>
<p><img src="../images/options_config_file_demo.png" alt="&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x6F14;&#x793A;"></p>
<h2 id="&#x8BF4;&#x660E;">&#x8BF4;&#x660E;</h2>
<h4 id="1-&#x65E5;&#x5FD7;">1. &#x65E5;&#x5FD7;</h4>
<p>&#x5F53;&#x6211;&#x4EEC;&#x5728;&#x4EE3;&#x7801;&#x4E2D;&#x8C03;&#x7528;parse_command_line()&#x6216;&#x8005;parse_config_file()&#x7684;&#x65B9;&#x6CD5;&#x65F6;&#xFF0C;tornado&#x4F1A;&#x9ED8;&#x8BA4;&#x4E3A;&#x6211;&#x4EEC;&#x914D;&#x7F6E;&#x6807;&#x51C6;logging&#x6A21;&#x5757;&#xFF0C;&#x5373;&#x9ED8;&#x8BA4;&#x5F00;&#x542F;&#x4E86;&#x65E5;&#x5FD7;&#x529F;&#x80FD;&#xFF0C;&#x5E76;&#x5411;&#x6807;&#x51C6;&#x8F93;&#x51FA;&#xFF08;&#x5C4F;&#x5E55;&#xFF09;&#x6253;&#x5370;&#x65E5;&#x5FD7;&#x4FE1;&#x606F;&#x3002;</p>
<p><img src="../images/log_info.png" alt="&#x65E5;&#x5FD7;&#x4FE1;&#x606F;"></p>
<p>&#x5982;&#x679C;&#x60F3;&#x5173;&#x95ED;tornado&#x9ED8;&#x8BA4;&#x7684;&#x65E5;&#x5FD7;&#x529F;&#x80FD;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x547D;&#x4EE4;&#x884C;&#x4E2D;&#x6DFB;&#x52A0;--logging=none &#x6216;&#x8005;&#x5728;&#x4EE3;&#x7801;&#x4E2D;&#x6267;&#x884C;&#x5982;&#x4E0B;&#x64CD;&#x4F5C;:</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> tornado.options <span class="hljs-keyword">import</span> options, parse_command_line
options.logging = <span class="hljs-keyword">None</span>
parse_command_line()
</code></pre>
<h4 id="2-&#x914D;&#x7F6E;&#x6587;&#x4EF6;">2. &#x914D;&#x7F6E;&#x6587;&#x4EF6;</h4>
<p>&#x6211;&#x4EEC;&#x770B;&#x5230;&#x5728;&#x4F7F;&#x7528;prase_config_file()&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x7684;&#x4E66;&#x5199;&#x683C;&#x5F0F;&#x4ECD;&#x9700;&#x8981;&#x6309;&#x7167;python&#x7684;&#x8BED;&#x6CD5;&#x8981;&#x6C42;&#xFF0C;&#x5176;&#x4F18;&#x52BF;&#x662F;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5C06;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x7684;&#x53C2;&#x6570;&#x8F6C;&#x6362;&#x8BBE;&#x7F6E;&#x5230;&#x5168;&#x5C40;&#x5BF9;&#x8C61;tornado.options.options&#x4E2D;&#xFF1B;&#x7136;&#x800C;&#xFF0C;&#x5176;&#x4E0D;&#x65B9;&#x4FBF;&#x7684;&#x5730;&#x65B9;&#x5728;&#x4E8E;&#x9700;&#x8981;&#x5728;&#x4EE3;&#x7801;&#x4E2D;&#x8C03;&#x7528;tornado.options.define()&#x6765;&#x5B9A;&#x4E49;&#x9009;&#x9879;&#xFF0C;&#x800C;&#x4E14;&#x4E0D;&#x652F;&#x6301;&#x5B57;&#x5178;&#x7C7B;&#x578B;&#xFF0C;&#x6545;&#x800C;&#x5728;&#x5B9E;&#x9645;&#x5E94;&#x7528;&#x4E2D;&#x5927;&#x90FD;&#x4E0D;&#x4F7F;&#x7528;&#x8FD9;&#x79CD;&#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x5728;&#x4F7F;&#x7528;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x901A;&#x5E38;&#x4F1A;&#x65B0;&#x5EFA;&#x4E00;&#x4E2A;python&#x6587;&#x4EF6;&#xFF08;&#x5982;config.py&#xFF09;&#xFF0C;&#x7136;&#x540E;&#x5728;&#x91CC;&#x9762;&#x76F4;&#x63A5;&#x5B9A;&#x4E49;python&#x7C7B;&#x578B;&#x7684;&#x53D8;&#x91CF;&#xFF08;&#x53EF;&#x4EE5;&#x662F;&#x5B57;&#x5178;&#x7C7B;&#x578B;&#xFF09;&#xFF1B;&#x5728;&#x9700;&#x8981;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x53C2;&#x6570;&#x7684;&#x5730;&#x65B9;&#xFF0C;&#x5C06;config.py&#x4F5C;&#x4E3A;&#x6A21;&#x5757;&#x5BFC;&#x5165;&#xFF0C;&#x5E76;&#x4F7F;&#x7528;&#x5176;&#x4E2D;&#x7684;&#x53D8;&#x91CF;&#x53C2;&#x6570;&#x3002;</p>
<p>&#x5982;config.py&#x6587;&#x4EF6;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># conding:utf-8</span>

<span class="hljs-comment"># Redis&#x914D;&#x7F6E;</span>
redis_options = {
    <span class="hljs-string">&apos;redis_host&apos;</span>:<span class="hljs-string">&apos;127.0.0.1&apos;</span>,
    <span class="hljs-string">&apos;redis_port&apos;</span>:<span class="hljs-number">6379</span>,
    <span class="hljs-string">&apos;redis_pass&apos;</span>:<span class="hljs-string">&apos;&apos;</span>,
}

<span class="hljs-comment"># Tornado app&#x914D;&#x7F6E;</span>
settings = {
    <span class="hljs-string">&apos;template_path&apos;</span>: os.path.join(os.path.dirname(__file__), <span class="hljs-string">&apos;templates&apos;</span>),
    <span class="hljs-string">&apos;static_path&apos;</span>: os.path.join(os.path.dirname(__file__), <span class="hljs-string">&apos;statics&apos;</span>),
    <span class="hljs-string">&apos;cookie_secret&apos;</span>:<span class="hljs-string">&apos;0Q1AKOKTQHqaa+N80XhYW7KCGskOUE2snCW06UIxXgI=&apos;</span>,
    <span class="hljs-string">&apos;xsrf_cookies&apos;</span>:<span class="hljs-keyword">False</span>,
    <span class="hljs-string">&apos;login_url&apos;</span>:<span class="hljs-string">&apos;/login&apos;</span>,
    <span class="hljs-string">&apos;debug&apos;</span>:<span class="hljs-keyword">True</span>,
}

<span class="hljs-comment"># &#x65E5;&#x5FD7;</span>
log_path = os.path.join(os.path.dirname(__file__), <span class="hljs-string">&apos;logs/log&apos;</span>)
</code></pre>
<p>&#x4F7F;&#x7528;config.py&#x7684;&#x6A21;&#x5757;&#x4E2D;&#x5BFC;&#x5165;config&#xFF0C;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-comment"># conding:utf-8</span>

<span class="hljs-keyword">import</span> tornado.web
<span class="hljs-keyword">import</span> config

<span class="hljs-keyword">if</span> __name__ = <span class="hljs-string">&quot;__main__&quot;</span>:
    app = tornado.web.Application([], **config.settings)
...
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../c02/s03.html" class="navigation navigation-prev " aria-label="Previous page: 2.3 httpserver"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../c02/s05.html" class="navigation navigation-next " aria-label="Next page: 2.5 练习"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
